<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');


/**
 * YekComputer (r) CMS controller class
 * 
 * @version: 1.0
 * @copyright (c) 2009, YekComputer. All rights reserved.
 * @author Babak Taraghi 'babak.taraghi@gmail.com'
 * @license Code licensed under the BSD License.
 */
class Products extends CI_Controller {
	
	
	/**
	 * @desc Array - datas which should pass to the views
	 */
	var $data = array();
	
	
	/**
	 * @desc Controller Constructor
	 */
	public function Products(){
		parent::__construct();
		$this->load->model("categoriesModel");
		$this->load->model("productsModel");
		
		$this->load->helper("form");
		
		$categories = $this->categoriesModel->getAllChildren($this->categoriesModel->getRecordByInternalName("products"));
		foreach($categories as $category){
			$this->data["categories"][] = $this->categoriesModel->getLangRecord($category->id,"en");
		}
	}
	
	
	public function index(){
		$this->inventory();
	}
	
	
	/**
	 * @desc This will get called when no method specified 
	 */
	function inventory($categoryId=0,$from=0,$lang="en"){
		
		$this->data["lang"] = $lang;
		
		$where = array(
			"language"=>$lang
		);
		
		if($categoryId != 0){
			$this->data["current_category_id"] = $where["category_id"] = $categoryId;
		}
		
		$this->_setPagingLinks($this->productsModel->getTotalRecordsNumber($where),8,4,"inventory/".$categoryId,$lang);
		
		$products = $this->productsModel->getRecords($where,$from,8,"title","asc");
		
		$this->load->helper("simple_image_helper");
		$this->load->helper("text_helper");
		$SI = new SimpleImage();
		
		foreach($products as &$product){
			$product->image = $SI->getThumbPathWidthHeight($product->image,95,95);
			$product->description = word_limiter($product->description, 25);
		}
		$this->data["products"] = $products;
 
		$this->data["content"] = $this->load->view("products",$this->data,TRUE);
		$this->load->view('container',$this->data);
	}
	
	
	function _setPagingLinks($totalRecs,$recsPerPage,$uriSegment,$extraURL,$lang){
		$this->load->library('pagination');
		$c = array(
			'base_url'=>base_url()."products/$extraURL",
			'total_rows'=> $totalRecs,
			'per_page'=> $recsPerPage,
			"uri_segment"=>$uriSegment,
			"num_links"=>3,
			"full_tag_open"=>"<div class='paging'>",
			"full_tag_close"=>"</div>",
			"first_link"=>($lang == "en")?"First":"Primero",
			"first_tag_open"=>"<div onclick='window.document.location = $(this).children(\"a\").attr(\"href\")' class='firstPage'>",
			"first_tag_close"=>"</div>",
			"last_link"=>($lang == "en")?"Last":"Ultimo",
			"last_tag_open"=>"<div onclick='window.document.location = $(this).children(\"a\").attr(\"href\")' class='firstPage'>",
			"last_tag_close"=>"</div>",
			"next_tag_open"=>"<div onclick='window.document.location = $(this).children(\"a\").attr(\"href\")' class='page'>",
			"next_tag_close"=>"</div>",
			"prev_tag_open"=>"<div onclick='window.document.location = $(this).children(\"a\").attr(\"href\")' class='page'>",
			"prev_tag_close"=>"</div>",
			"num_tag_open"=>"<div onclick='window.document.location = $(this).children(\"a\").attr(\"href\")' class='page'>",
			"num_tag_close"=>"</div>",
			'cur_tag_open'=>"<div class='currentPage'>",
			'cur_tag_close'=>"</div>"
		);
		$this->pagination->initialize($c);
		$this->data["pagination"] = $this->pagination->create_links();
	}
	
	function item($langRelation,$lang="en"){

		$items = $this->productsModel->getRecords(array("language_relation"=>$langRelation,"language"=>$lang));
		if(count($items) > 0){
			$this->data["item"] = $items[0];
			$this->data["current_category_id"] = $this->data["item"]->category_id;
			
			$this->load->helper("simple_image_helper");
			$SI = new SimpleImage();
			
			$this->data["item"]->thumb = $SI->getThumbPathWidthHeight($this->data["item"]->image,260,260);
		}
		$this->data["lang"] = $lang;
		
		$this->data["content"] = $this->load->view("products",$this->data,TRUE);
		$this->load->view('container',$this->data);
	}
	
	
	
	function search($lang="en"){
		
		$keywords = $this->input->post("keywords");
		$this->data["lang"] = $lang;
		
		if(!empty($keywords) && $keywords != "جستجو"){
			$where["title"] = $keywords;
			$where["description"] = $keywords;
			$where["specification"] = $keywords;
			$products = $this->productsModel->searchRecords($where,0,32,"title","asc");
			
			$this->load->helper("simple_image_helper");
			$this->load->helper("text_helper");
			$SI = new SimpleImage();
			
			foreach($products as &$product){
				$product->image = $SI->getThumbPathWidthHeight($product->image,95,95);
				$product->description = word_limiter($product->description, 30);
			}
			$this->data["products"] = $products;
		}

		$this->data["pagination"] = "";
		$this->data["content"] = $this->load->view("products",$this->data,TRUE);
		$this->load->view('container',$this->data);
			
	}
	
}
